
* Replication dofile 
* Ms. No. 230032R2: “Using Social Media to Respond to Negative Polls: Politicians’ Issue Responsiveness on Facebook”
* The Journal of Politics
* 17 January 2024

************
* Figure 1 *
use "dataset 2.dta", clear
graph hbox posts, over(issue, label(labcolor("black") labsize(medsmall))) box(1, fcolor(none) lcolor(black) lpattern(solid)) nooutsides medtype(cline) medline(lcolor(black) lwidth(thick)) ytitle(Facebook posts (mean) per politician over 52 weeks) note("")

************
* Figure 2 *
use "dataset 3.dta", clear
twoway (line poll week_poll if party == 110, sort lcolor(black) lwidth(medthick) lpattern(shortdash_dot)) ///
	   (line poll week_poll if party == 210, sort lcolor(black) lwidth(medthick) lpattern(vshortdash)) ///
	   (line poll week_poll if party == 220, sort lcolor(black) lwidth(med) lpattern(dash)) ///
	   (line poll week_poll if party == 410, sort lcolor(black) lwidth(medthick) lpattern(solid)) ///
	   (line poll week_poll if party == 430, sort lcolor(black) lwidth(medthick) lpattern(dash_dot)) ///
	   (line poll week_poll if party == 620, sort lcolor(black) lwidth(thin) lpattern(solid)) ///
	   (line poll week_poll if party == 720, sort lcolor(black) lwidth(thick) lpattern(dot)) ///
			if poll < 10, ytitle(Poll (voter support, %)) ytitle(, size(medlarge) color(black)) ylabel(, labsize(medlarge)) xtitle(Months) xtitle(, size(medlarge)) xlabel(0(2)12, labsize(medlarge)) ///
			legend(lab (1 "Å") lab(2 "Ø") lab(3 "SF") lab(4 "RV") lab(5 "LA") lab(6 "C") lab(7 "NB"))
			
twoway (line poll week_poll if party == 320, sort lcolor(black) lwidth(medthick) lpattern(longdash)) ///
	   (line poll week_poll if party == 420, sort lcolor(black) lwidth(medthick) lpattern(solid)) ///
	   (line poll week_poll if party == 700, sort lcolor(black) lwidth(medthick) lpattern(dash)) ///
			if poll > 10, ytitle(Poll (voter support, %)) ytitle(, size(medlarge) color(black)) ylabel(, labsize(medlarge)) xtitle(Months) xtitle(, size(medlarge)) xlabel(0(2)12, labsize(medlarge)) ///
			legend(lab (1 "SD") lab(2 "V") lab(3 "DF"))
			
*******************************************************
* Tables 1, 2, A2, A3, A5, A6, A11, A13. Figures 3, 4 *
use "dataset 1.dta", clear
	
keep if counter_numerical < 3 // We transform the data to only analyze the average values two periods before and after the cutt-off point
collapse prognose match poll_diff issue_posts party poll_neg_accum poll_diff_average poll_number poll_category_large2 issue_ownership* marginality_10 marginality_pers Gender first Birthyear meanIOscore_all, by(ID_Pol week_poll prepost_poll)
replace prognose = 1 if prognose > 0 & prognose < 2 // We clean the variable after the collapse command.
g panel = ID_P*100+week_poll // We set up the panel structure of the data that we need to include party and poll fixed effects to the analysis.
xtset panel prepost_poll

* Table 1, Table A2
zip match i.prepost_poll##c.poll_diff 		 					    	poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 
zip match i.prepost_poll##c.poll_diff 		 					    	poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(cluster party) infl(i.prepost_poll##c.poll_diff) 
zip match i.prepost_poll##c.poll_diff 		 					   i.ID poll_neg_accum poll_diff_average issue_posts poll_number i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff)
zip match i.prepost_poll##c.poll_diff 		     Gender first Birthyear poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 
zip match i.prepost_poll##c.poll_category_large2 		 				poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 

* Table 2, Table A3
zip match i.prepost_poll##c.poll_diff##c.marginality_pers 		 			poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(prepost_poll poll_diff) 
zip match i.prepost_poll##c.poll_diff##c.marginality_10 		 			poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(prepost_poll poll_diff) 
zip match i.prepost_poll##c.poll_diff##i.prognose							poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(prepost_poll poll_diff) 
zip match i.prepost_poll##c.poll_diff##c.issue_ownership		 			poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(prepost_poll poll_diff) 

* Figure 3 
quietly zip match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts i.party i.poll_number, vce(robust) infl(i.prepost_poll c.poll_diff) 
quietly margins, dydx(i.prepost) at(c.poll_diff=(-1.7(.01)1.7)) predict(xb) vsquish post
marginsplot, xdimension(at(poll_diff)) recast(line) plotopts(lcolor(black) lwidth(thick) lpattern(solid)) ///
	recastci(rline) ciopts(lcolor(black) lwidth(med) lpattern(dash)) ///
	addplot(histogram poll_diff, fcolor(gs10%60) lcolor(gs10%60) below yaxis(2) yscale(alt axis(2))) ///	
	ytitle(Predicted alignment, size(large) color(black) margin(vsmall)) ///
	ylabel(-0.4(0.2)0.4, labels labsize(medlarge) labcolor(black)) ///
	yline(0, lwidth(thin) lpattern(dash) lcolor(black)) ///
	xtitle(∆Poll, size(large) color(black)) ///
	xlabel(, labels labsize(medlarge) labcolor(black)) ///
	xline(0, lwidth(thin) lpattern(dash) lcolor(black)) title(" ") legend(off)
	
* Figure 4a
quietly zip match i.prepost_poll##c.poll_diff##c.issue_ownership poll_diff_average issue_posts i.party i.poll_number, vce(robust) infl(i.prepost_poll c.poll_diff) 
quietly margins, dydx(i.prepost) at(c.poll_diff=(-1.7(.01)1.7) issue_ownership=1.5) predict(xb) vsquish post
marginsplot, xdimension(at(poll_diff)) recast(line) plotopts(lcolor(black) lwidth(thick) lpattern(solid)) ///
	recastci(rline) ciopts(lcolor(black) lwidth(med) lpattern(dash)) ///
	addplot(histogram poll_diff, fcolor(gs10%60) lcolor(gs10%60) below yaxis(2) yscale(alt axis(2))) ///	
	ytitle(Predicted alignment, size(large) color(black) margin(vsmall)) ///
	ylabel(-1(0.5)1, labels labsize(medlarge) labcolor(black)) ///
	yline(0, lwidth(thin) lpattern(dash) lcolor(black)) ///
	xtitle(∆Poll, size(large) color(black)) ///
	xlabel(, labels labsize(medlarge) labcolor(black)) ///
	xline(0, lwidth(thin) lpattern(dash) lcolor(black)) title(" ") legend(off)

* Figure 4b
quietly zip match i.prepost_poll##c.poll_diff##c.issue_ownership poll_diff_average issue_posts i.party i.poll_number, vce(robust) infl(i.prepost_poll c.poll_diff) 
quietly margins, dydx(i.prepost) at(c.poll_diff=(-1.7(.01)1.7) issue_ownership=3.5) predict(xb) vsquish post
marginsplot, xdimension(at(poll_diff)) recast(line) plotopts(lcolor(black) lwidth(thick) lpattern(solid)) ///
	recastci(rline) ciopts(lcolor(black) lwidth(med) lpattern(dash)) ///
	addplot(histogram poll_diff, fcolor(gs10%60) lcolor(gs10%60) below yaxis(2) yscale(alt axis(2))) ///	
	ytitle(Predicted alignment, size(large) color(black) margin(vsmall)) ///
	ylabel(-1(0.5)1, labels labsize(medlarge) labcolor(black)) ///
	yline(0, lwidth(thin) lpattern(solid) lcolor(black)) ///
	xtitle(∆Poll, size(large) color(black)) ///
	xlabel(, labels labsize(medlarge) labcolor(black)) ///
	title(" ") legend(off)
	
* Table A5
zip 	match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 
zinb 	match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 
poisson match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust)
xtreg 	match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust)
nbreg 	match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust)

* Table A6
g match2 = match // We generate a dicotomous dependent variable
	recode match2 (0.1/5=1)
logit match2 i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) 

* Table A10
zip match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number if poll_diff > -1 & poll_diff < 1, vce(robust) infl(i.prepost_poll##c.poll_diff) // We only analyze poll changes larger than +/- 1 pct. point. 

* Table A12
zip match i.prepost_poll##c.poll_diff##c.meanIOscore_all poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 
recode meanIOscore_all (0/5=0)(5.001/14.8=1) // We recode the issue emphasis variable to a dichotomous variable based on the median value
zip match i.prepost_poll##c.poll_diff##c.meanIOscore_all poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 

***********************
* Figure A2, Table A7 *
use "dataset 1.dta", clear
replace counter = counter+5 // We change the time variable to only have positive values in order to conduct the analysis
zip match i.DID_treatment##ib5.counter i.party i.week_poll, vce(robust) infl(DID_treatment)
coefplot, keep(*#*) vertical yline(0) xline(4.5) xtitle(Timeline) msymbol(square) mcolor(black) msize(med) ///
	coeflabel(1.DID_treatment#1.counter = "t-4" ///
			  1.DID_treatment#2.counter = "t-3" ///
			  1.DID_treatment#3.counter = "t-2" ///
			  1.DID_treatment#4.counter = "t-1" ///
			  1.DID_treatment#6.counter = "t+1" ///
			  1.DID_treatment#7.counter = "t+2" ///
			  1.DID_treatment#8.counter = "t+3" ///
			  1.DID_treatment#9.counter = "t+4")
	
*****************
* Figure A1 (A) *
use "dataset 1.dta", clear
drop if counter > 2 // We limit the analysis to the two periods before and after the cut-off point.
drop if counter < -2
collapse match, by(DID_treatment counter)
twoway (line match counter if DID_treatment==1, lcolor(black) lwidth(thick) lpattern(solid)) ///
	   (line match counter if DID_treatment==0, lcolor(black) lwidth(thick) lpattern (dash)), ///
	   xline(0, lwidth(thin) lpattern(solid) lcolor(black)) ///
	   ytitle(Match, size(large) color(black)) xtitle(Timeline, size(large) color(black)) legend(off) ///
	   xlabel(, labels labsize(medlarge) labcolor(black)) ylabel(.4(.05).6, labels labsize(medlarge) labcolor(black)) 
 
*****************
* Figure A1 (B) *
use "dataset 1.dta", clear
replace counter = counter + 2 // For the placebo test, we create an artificial cut-off point at t+2.
drop if counter > 2 // We limit the analysis to the two periods before and after the cut-off point.
drop if counter < -2
collapse match, by(DID_treatment counter) // We transform the data to only analyze the average values two periods before and after the cutt-off point.
twoway (line match counter if DID_treatment==1, lcolor(black) lwidth(thick) lpattern(solid)) ///
	   (line match counter if DID_treatment==0, lcolor(black) lwidth(thick) lpattern (dash)), ///
	   xline(0, lwidth(thin) lpattern(solid) lcolor(black)) ///
	   ytitle(Match, size(large) color(black)) xtitle(Timeline, size(large) color(black)) legend(off) ///
	   xlabel(, labels labsize(medlarge) labcolor(black)) ylabel(.4(.05).6, labels labsize(medlarge) labcolor(black)) 

*****************
* Figure A1 (C) *
use "dataset 1.dta", clear
replace counter = counter - 2 // For the placebo test, we create an artificial cut-off point at t-2.
drop if counter > 2 // We limit the analysis to the two periods before and after the cut-off point.
drop if counter < -2
collapse match, by(DID_treatment counter)
twoway (line match counter if DID_treatment==1, lcolor(black) lwidth(thick) lpattern(solid)) ///
	   (line match counter if DID_treatment==0, lcolor(black) lwidth(thick) lpattern (dash)), ///
	   xline(0, lwidth(thin) lpattern(solid) lcolor(black)) ///
	   ytitle(Match, size(large) color(black)) xtitle(Timeline, size(large) color(black)) legend(off) ///
	   xlabel(, labels labsize(medlarge) labcolor(black)) ylabel(.4(.05).6, labels labsize(medlarge) labcolor(black)) 

	   
**********************************
* Figures A3, A4. Tables A9, A10 * 
use "dataset 1.dta", clear
keep if counter_numerical < 3  // We transform the data to only analyze the average values two periods before and after the cutt-off point.
collapse prognose* match poll_diff issue_posts party poll_neg_accum poll_diff_average poll_number poll_category_large2 issue_ownership* marginality_10 marginality_pers Gender first Birthyear meanIOscore_all, by(ID_Pol week_poll prepost_poll)
g panel = ID_P*100+week_poll // We set up the panel structure of the data that we need to include fixed effects to the analysis.
xtset panel prepost_poll
tab party, g(d_party) // generate dummies for each party
tab week_poll, g(d_week_poll) // generate dummies for each poll

* Figure A3
*ssc install rddensity // We install the Stata package "rddensity & rdrobust" to conduct these analyses. 
*ssc install rdrobust // lpdensity rdplot 

rdplot match poll_diff if match < 0.8, c(0) all masspoints(off) p(1) bwselect(mserd) kernel(uni) covs(d_party* d_week_poll* issue_posts) 

* Figure A4
rddensity poll_diff, fitselect(restricted) hist_n(7 7) p(3) h(4 4) all plot 
	
* Table A8 
rdrobust match poll_diff, c(0) all masspoints(off) p(1) bwselect(mserd) kernel(uni)

* Table A9 
rdrobust Gender poll_diff, c(0) all masspoints(off) p(1) bwselect(mserd) kernel(uni)
rdrobust Birthyear poll_diff, c(0) all masspoints(off) p(1) bwselect(mserd) kernel(uni)


************
* Table A4 *

* Model 1
use "dataset 4.dta", clear
keep if counter_numerical < 3  // We transform the data to only analyze the average values two periods before and after the cutt-off point.
collapse match poll_diff issue_posts party poll_neg_accum poll_diff_average poll_number, by(ID_Pol week_poll prepost_poll)
g panel = ID_P*100+week_poll // We set up the panel structure of the data that we need to include fixed effects to the analysis.
xtset panel prepost_poll
zip match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff)

* Model 2
* This model is the same as model 1 in Table 1 in manuscript (line 57 in do-file)

* Model 3
use "dataset 5.dta", clear
keep if counter_numerical < 3  // We transform the data to only analyze the average values two periods before and after the cutt-off point.
collapse match poll_diff issue_posts party poll_neg_accum poll_diff_average poll_number, by(ID_Pol week_poll prepost_poll)
g panel = ID_P*100+week_poll // We set up the panel structure of the data that we need to include fixed effects to the analysis.
xtset panel prepost_poll
zip match i.prepost_poll##c.poll_diff poll_neg_accum poll_diff_average issue_posts poll_number i.party i.poll_number, vce(robust) infl(i.prepost_poll##c.poll_diff) 
 
*************
* Figure A5 *
use "dataset 6.dta", clear
graph hbar (mean) IO_pct, over(party, sort(IO_pct) descending) by(issue) ytitle(Issue ownership score (%))

******************
* Figures A6, A7 *
use "dataset 7.dta", clear

* Figure A6
graph bar (mean) attention, over(party, sort(attention) descending) by(CAP) ytitle(Issue Emphasis (%))

reshape wide attention, i(party year) j(CAP) // We reshape the data to be able to do timeseries graphs for specific issues.
xtset party year

* Figure A7a
xtline attention3, ytitle(Emphasis (%)) ttitle(" ") ylabel(0(10)30, labels labsize(medlarge) labcolor(black)) xlabel(, labels labsize(medlarge) labcolor(black)) byopts(title(Health, size(medlarge) color(black))) subtitle(, size(medlarge) nobox) byopts(legend(off))
* Figure A7b
xtline attention6, ytitle(Emphasis (%)) ttitle(" ") ylabel(0(25)50, labels labsize(medlarge) labcolor(black)) xlabel(, labels labsize(medlarge) labcolor(black)) byopts(title(Education, size(medlarge) color(black))) subtitle(, size(medlarge) nobox) byopts(legend(off))
* Figure A7c
xtline attention7, ytitle(Emphasis (%)) ttitle(" ") ylabel(0(5)20, labels labsize(medlarge) labcolor(black)) xlabel(, labels labsize(medlarge) labcolor(black)) byopts(title(Environment, size(medlarge) color(black))) subtitle(, size(medlarge) nobox) byopts(legend(off))
* Figure A7d
xtline attention9, ytitle(Emphasis (%)) ttitle(" ") ylabel(0(10)30, labels labsize(medlarge) labcolor(black)) xlabel(, labels labsize(medlarge) labcolor(black)) byopts(title(Immigration, size(medlarge) color(black))) subtitle(, size(medlarge) nobox) byopts(legend(off))
